<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-public-scheduling" :style="getStyle('area')">
      <div class="scheduling-wrap">
        <template v-for="(item) in doctorSchedulingInfo">
          <div class="group-first">
            <template v-for="(itemInfo,key) in item">
              <div class="doctor-info dept-info" v-if="parseInt(itemInfo.type)===2">
                <div class="dept-name" :style="getStyle('deptName')">{{ itemInfo?.deptVO?.deptName }}</div>
                <div class="campus-area" :style="getStyle('campus')">
                  <template v-for="deptItem in hospitalVOList(itemInfo?.deptVO['hospitalVOList'])">
                    <div class="item">
                      <template v-if="deptItem.hospitalLogo">
                        <img
                            alt="院区标志"
                            class="icon-picture"
                            :src="deptItem.hospitalLogo"
                            :class="getIconName(deptItem.hospitalLogo)"
                        />
                      </template>
                      {{ deptItem['hospitalName'] }}
                    </div>
                  </template>
                </div>
              </div>
              <div class="doctor-info" v-else>
                <template v-if="itemInfo?.doctorVO?.doctorName">
                  <div class="left-area">
                    <img :src="itemInfo?.doctorVO?.doctorPhoto" alt="医生" v-on:error="handleError">
                  </div>
                  <div class="right-area">
                    <div class="doctor-name" v-if="true">
                      <div class="name">{{ itemInfo?.doctorVO?.doctorName || '\u00a0' }}</div>
                      <div class="level" v-if="itemInfo?.doctorVO?.title">
                        <div class="duty item" v-if="false">
                          <!--                          <ScrollX>-->
                          {{ itemInfo?.doctorVO?.duty || '\u00a0' }}
                          <!--                          </ScrollX>-->
                        </div>
                        <div class="title item" v-if="true">
                          <ScrollX>
                            {{ itemInfo?.doctorVO?.title || '\u00a0' }}
                          </ScrollX>
                        </div>
                      </div>
                    </div>
                    <div class="doctor-describe" v-if="true">
                      <div class="container">
                        <ScrollY :content="itemInfo?.doctorVO?.intro || '\u00a0'" :pageDuration="10"/>
                      </div>
                    </div>
                    <!--排版的界面-->
                    <div class="doctor-scheduling">
                      <div class="scheduling-inside">
                        <div class="header">
                          <template v-if="true">
                            <div class="item"
                                 v-for="(campusItem, index) in schedulingInfo(itemInfo?.doctorVO?.doctorSchedule)"
                                 :key="index">
                              <!--                              {{campusItem}}-->
                              <template v-if="campusItem?.label?.name ==='午别'">
                                <div class="label item-info">{{ campusItem?.label?.name || '\u00a0' }}</div>
                                <div class="am item-info">{{ campusItem?.AM?.name || '\u00a0' }}</div>
                                <div class="pm item-info">{{ campusItem?.PM?.name || '\u00a0' }}</div>
                              </template>
                              <template v-else>
                                <div class="label item-info">{{ campusItem?.label?.name || '\u00a0' }}</div>
                                <div class="am item-info">
                                  <!--                                  <i class="icon el-icon-star-on" v-if="campusItem?.AM?.value ===1"></i>-->
                                  <!--                                  <i class="icon el-icon-question" v-if="campusItem?.AM?.value ===2"></i>-->
                                  <!--                                  <i class="icon el-icon-warning" v-if="campusItem?.AM?.value ===3"></i>-->
                                  <!--                                  <i class="icon el-icon-success" v-if="campusItem?.AM?.value ===4"></i>-->
                                  <img class="campus-picture"
                                       alt="院区标志"
                                       :src="campusSignInfo(campusItem?.AM)"
                                       v-if="campusSignInfo(campusItem?.AM)"/>
                                </div>
                                <div class="pm item-info">
                                  <img class="campus-picture"
                                       alt="院区标志"
                                       :src="campusSignInfo(campusItem?.PM)"
                                       v-if="campusSignInfo(campusItem?.PM)"/>
                                </div>
                              </template>
                            </div>
                          </template>

                        </div>
                      </div>
                    </div>
                  </div>
                </template>
              </div>
            </template>
          </div>
        </template>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import SvgIcon from "@c/public/svg-icon/index.vue";
import ScrollY from "@c/public/seamlessScroll/ScrollY.vue";
import {getSchedulingDoctorInfo} from "@/store/modules/getters";

export default {
  name: "scene-public-scheduling-21-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollY,
    SvgIcon,
    ScrollX,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      doctorSchedule: [
        {
          label: {name: '午别', value: 0},
          AM: {name: '上午', value: 0},
          PM: {name: '下午', value: 0}
        },
        {
          label: {name: '周一', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
        {
          label: {name: '周二', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
        {
          label: {name: '周三', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
        {
          label: {name: '周四', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
        {
          label: {name: '周五', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
        {
          label: {name: '周六', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
        {
          label: {name: '周日', value: 1},
          AM: {name: '上午', value: ''},
          PM: {name: '下午', value: ''}
        },
      ],
      optionsInfo: Array(),
      header2: require("@/components/assets/images/design/header.jpeg"),
      header: require("@a/images/design/header.jpeg"),
    }
  },


  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        let dept = (info.dept);
        switch (val) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(info.areaHeight));
            return Obj;
          case "deptName":
            Reflect.set(Obj, "color", (dept['deptName'].color));
            Reflect.set(Obj, "fontWeight", (dept['deptName'].fontWeight));
            return Obj;
          case "campus":
            Reflect.set(Obj, "color", (dept['campus'].color));
            Reflect.set(Obj, "fontWeight", (dept['campus'].fontWeight));
            return Obj;
        }
      };
    },

    //获取医生信息
    doctorSchedulingInfo() {
      let list;
      let devType = (this.devType);
      let {patientListRow, patientListCol} = this.optionsInfo;
      let doctorListInfo = this.$store.getters['getSchedulingDoctorInfo'];
      if (devType) {
        list = doctorListInfo['apiData'] || []
      } else {
        list = doctorListInfo['mockData'] || []
      }
      let addArr = Array();
      let flattenArr = Array();
      if (list?.length >= (patientListRow * patientListCol)) {
        flattenArr = list.slice(0, (patientListRow * patientListCol));
      } else {
        addArr = (new Array((patientListRow * patientListCol) - list.length).fill({}));
        flattenArr = [...list, ...addArr];
      }
      return this.$lodash.chunk(flattenArr, patientListCol)
    },

    //院区图标
    getIconName() {
      return function (type) {
        switch (type) {
          case 1:
            return 'el-icon-star-on'
          case 2:
            return 'el-icon-question'
          case 3:
            return 'el-icon-warning'
          case 4:
            return 'el-icon-success'
        }
      }
    },

    // 获取排班的logo
    campusSignInfo() {
      return function (params) {
        if (Reflect.has(params, 'campus')) {
          return params?.campus?.hospitalLogo || ''
        } else {
          return ''
        }
      }
    },

    //获取医生信息
    schedulingInfo() {
      return function (row) {
        const self = this;
        let obj;
        if (self.$lodash.isString(row)) {
          try {
            let objInfo = JSON.parse(row);
            if (self.$lodash.isEmpty(objInfo)) {
              obj = self.doctorSchedule;
            } else {
              obj = objInfo;
            }
          } catch (e) {
            obj = self.doctorSchedule;
          }
          return obj
        } else {
          if (row?.length) {
            return row
          } else {
            return this.doctorSchedule
          }
        }
      }
    },


    //院区选择
    hospitalVOList() {
      return function (row) {
        if (this.$lodash.isArray(row)) {
          if (row?.length >= 4) {
            return row.slice(0, 4)
          } else {
            let arr = (new Array(4 - row?.length).fill({}));
            return [...row, ...arr];
          }
        } else {
          return Array()
        }
      }
    },

  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
          let publicListInfo = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setSchedulingDoctorInfo', publicListInfo)
          break
      }
    },


    handleError(event) {
      event.target.src = '/tpleditor/resource/assets/images/header.png'
    }
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
$scale: 7;
.scene-public-scheduling {
  background-color: transparent;
  color: #333;
  height: 100%;
  line-height: 100%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  box-sizing: border-box;
  //transform: scale(3) translate(calc(16.5% * 2), calc(16.5% * 2));

  .scheduling-wrap {
    height: calc(100%);
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .group-first {
      display: flex;
      flex-shrink: 0;
      //垂直方向平分
      flex-grow: 1;
      max-height: calc(100% / 3);
      min-height: calc(100% / 3);
      overflow: hidden;

      .doctor-info {
        flex: 1;
        flex-shrink: 0;
        background-color: rgba(229, 234, 239, 1);
        @include background('design/bg5-1×2');
        display: flex;
        overflow: hidden;
        padding: 20px 16px;
        min-width: calc(100% / 7);
        max-width: calc(100% / 7);
        box-sizing: border-box;

        .left-area {
          width: 80px;
          height: 108px;
          border-radius: 4px;
          overflow: hidden;
          flex-shrink: 0;
          border: calc(2px / 2) #dcdee2 solid;

          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }

        .right-area {
          margin-left: 8px;
          overflow: hidden;
          flex-grow: 1;

          .doctor-name {
            width: 100%;
            font-size: 16px;
            display: flex;
            flex-direction: row;

            .name {
              font-size: 16px;
              line-height: initial;
              white-space: nowrap;
              color: black;
              margin-right: 6px;
              overflow: hidden;
              @include singleEllipsis(72px);
              box-sizing: border-box;
            }

            .level {
              font-size: 8px;
              color: white;
              display: flex;
              align-self: center;
              justify-content: flex-start;
              width: calc(100% - 72px);
              min-width: calc(100% - 72px);
              line-height: 1.45;
              box-sizing: border-box;

              .item {
                white-space: nowrap;
                background: #0059B3;
                align-self: center;
                max-width: 100%;
                min-width: 50%;
                border-radius: calc(4px / 2);
                padding: calc(4px / 2) calc(8px / 2);
                box-sizing: border-box;
                overflow: hidden;
              }

              .duty {
                margin-right: 4px;
                box-sizing: border-box;
              }

            }
          }

          .doctor-describe {
            display: flex;
            justify-content: space-around;
            width: 100%;
            height: 40px;
            margin-top: 4px;

            .container {
              width: 100%;
              height: 100%;
              line-height: 1.25;
              box-sizing: border-box;
              font-size: 12px;
              color: rgba(51, 51, 51, 0.8);
              text-align: justify;
            }
          }

          .doctor-scheduling {
            width: 100%;
            margin-top: 8px;
            align-items: end;
            align-self: flex-end;

            .scheduling-inside {
              width: 100%;

              .header {
                display: flex;
                line-height: initial;
                overflow: hidden;
                background: #FFFFFF;
                border: 1PX rgba(186, 187, 189, 0.8) solid;
                border-radius: calc(4px / 2) calc(4px / 2);

                .item {
                  flex: 1;
                  font-size: 6px;
                  text-align: center;
                  border-radius: calc(4px / 2) calc(4px / 2);
                  border-right: 1PX rgba(186, 187, 189, 0.8) solid;
                  justify-content: center;

                  &:last-child {
                    border-right: none;
                  }

                  .item-info {
                    overflow: hidden;
                    height: 10px;
                    line-height: 10px;
                    align-items: center;
                    align-self: center;
                    justify-content: center;
                  }

                  .am {
                    border-top: 1PX rgba(186, 187, 189, 0.8) solid;
                    border-bottom: 1PX rgba(186, 187, 189, 0.8) solid;
                    display: flex;

                    .campus-picture {
                      width: 8px;
                      height: 8px;
                      object-fit: contain;
                    }
                  }

                  .pm {
                    display: flex;

                    .campus-picture {
                      width: 8px;
                      height: 8px;
                      object-fit: contain;
                    }
                  }
                }
              }
            }
          }
        }
      }

      .dept-info {
        padding-top: 30px;
        display: flex;
        flex-shrink: 0;

        box-sizing: border-box;
        flex-direction: column;
        overflow: hidden;

        .dept-name {
          height: 58px;
          color: #FFFFFF;
          display: flex;
          width: 100%;
          align-self: center;
          align-items: center;
          justify-content: center;
          font-size: 30px;
          box-sizing: border-box;
          @include background('design/dept-1×2');
        }

        .campus-area {
          display: flex;
          flex-direction: row;
          flex-wrap: wrap;
          overflow: hidden;
          margin-top: 8px;
          flex-grow: 1;
          padding: 0 12px;
          box-sizing: border-box;

          .item {
            flex-basis: 50%;
            display: flex;
            align-self: center;
            padding-left: 36px;
            box-sizing: border-box;
            font-size: 12px;
            line-height: 1.5;
            position: relative;

            .icon-picture {
              width: 12px;
              height: 12px;
              position: absolute;
              top: 2px;
              left: 18px;
            }

            .el-icon-star-on {
              font-size: 10px;
              position: absolute;
              color: rgba(229, 134, 2, 1);
              top: 0;
            }
          }
        }
      }
    }
  }
}
</style>